<?php $this->load->view('_inc/header');?>

<script>
    $(document).ready(function(){
       function decideUrlEdicao(nome, acao){
            switch(nome){
                case "mej": return acao +'Mej/'; break;
                case "cargo": return acao +'Cargo/'; break;
            }
        }
        
        $('.cadastrar').click(function(){
            var divEditar = $('.popUp');
            var nome = $(this).attr('id');
            $.ajax({
                url: '<?php echo base_url()?>index.php/apoio/cadastrar' + nome,
                type: 'POST',
                success: function(data){
                    divEditar.html(data);
                    divEditar.fadeIn(600);
                }
            });
        }); 
        
        $('.editar').click(function(){
            var id = $(this).attr('id');
            var nome = $(this).attr('name');
            var acao = 'editar';
            var novaUrl = decideUrlEdicao(nome, acao);
            var divEditar = $('.popUp');
            $.ajax({
                url: '<?php echo base_url()?>index.php/apoio/' + novaUrl + id,
                type: 'POST',
                success: function(data){
                    divEditar.html(data);
                    abrePopUp();
                }
            });
        });
        
        $('.excluir').click(function(){
            var id = $(this).attr('id');
            var nome = $(this).attr('name');
            var acao = 'excluir';
            var novaUrl = decideUrlEdicao(nome, acao);
            $(this).parent('td').parent('tr').css('background','#ff9999').hide(600);
            $.ajax({
                url: '<?php echo base_url()?>index.php/apoio/' + novaUrl + id,
                type: 'POST',
            });
        });
        
        $('.pontuar').click(function(){
            var id = $(this).attr('id');
            var divEditar = $('.popUp');
            $.ajax({
                url: '<?php echo base_url()?>index.php/apoio/adicionarPontosCargos/' + id,
                type: 'POST',
                success: function(data){
                    divEditar.html(data);
                    abrePopUp();
                }
            });
        });
    });
</script>

<button class="cadastrar" id="Mej">Nova Entidade</button>
<button class="cadastrar" id="Cargo">Novo Cargo</button>

<div id="areaTroca"></div>

<table class="gradeamento">
<tr>
<td>
    <h1>Entidades</h1>
    <table class="listagem">
        <tr>
            <th>Nome</th>
        </tr>
        <?php foreach ($mejs as $mej){?>
            <tr>
                <td><?php echo $mej->mej?></td>
                <td style="text-align: right">
                    <a id="<?php echo $mej->id?>" class="icone pontuar" title="Adicionar Pontos"></a>
                    <a id="<?php echo $mej->id?>" name="mej" class="icone editar" title="Editar"></a>
                    <a id="<?php echo $mej->id?>" name="mej" class="icone excluir" title="Excluir"></a>
                </td>
            </tr>
        <?php }?>
    </table>
</td>


<td>
    <h1>Cargos</h1>
    <table class="listagem">
        <tr>
            <th>Nome</th>
        </tr>
        <?php foreach ($cargos as $cargo){?>
            <tr>
                <td colspan=10><?php echo $cargo->cargo?></td>
                <td style="text-align: right">
                    <a id="<?php echo $cargo->id?>" name="cargo" class="icone editar" title="Editar"></a>
                    <a id="<?php echo $cargo->id?>" name="cargo" class="icone excluir" title="Excluir"></a>
                </td>
            </tr>
        <?php }?>
    </table>
</td>
</tr>
</table>

<?php $this->load->view('_inc/footer');?>